/*
 * @Author: your name
 * @Date: 2022-03-28 15:57:47
 * @LastEditTime: 2022-04-27 03:10:38
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \react-antd-admin\vue-admin-template\src\global.js
 */

export function buildTree(data) {
  const res = [];
  for (const item of data) {
    if (!item.parent_id) {
      item.children = getNode(item.id);
      res.push(item);
    }
  }

  function getNode(id) {
    const node = [];
    for (const item of data) {
      if (item.parent_id === id) {
        item.children = getNode(item.id);
        node.push(item);
      }
    }
    if (node.length === 0) return [];
    return node;
  }
  return res;
}